<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 打印n以内所有偶数的和 封装函数 n 是参数； -->
    <script>
        function fn(num) {
            var sum = 0;
            for (var i = 0; i <= num; i++) {
                if (i % 2 == 0) {
                    sum += i;
                }
            }
            console.log(sum);
        }
        fn(prompt("请输入")); //调用函数
    </script>
</body>

</html>